package com.tosit.bmx.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tosit.bmx.common.json.JSONModel;
import com.tosit.bmx.common.json.JSONUtil;
import com.tosit.bmx.domian.CartypeVoEnhance;
import com.tosit.bmx.domian.Subject;
import com.tosit.bmx.domian.SubjectVo;
import com.tosit.bmx.service.admin.CarManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 考试题目管理类
 *
 * @author tsl
 * @create 2018-04-24 14:20
 **/
@Controller
@RequestMapping()
public class SubjectController {
    @Autowired
    CarManagerService carManagerService;

    /**
     * 添加一个题
     *
     * @param subject
    //     * @param readfile  读题音频
    //     * @param examfile  解析音频
    //     * @param zreadfile 藏读题音频
    //     * @param zexamfile 藏解析音频
    //     * @param mfile  媒体
    //     * @param emfile 解释媒体
     * @param request
     * @param response
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @RequestMapping(value = "/subject", method = RequestMethod.POST)
    public String addSubject(Subject subject,
                             HttpServletRequest request, HttpServletResponse response) {
        Integer result = null;
        MultipartHttpServletRequest murequest=(MultipartHttpServletRequest)request;
        MultipartFile readfile = murequest.getFile("readfile");
        result = carManagerService.addSubject(subject);
        if(!readfile.isEmpty()) {
            //上传文件路径
            String path = request.getServletContext().getRealPath("/upload/music/");
            //上传文件名
            String filename = readfile.getOriginalFilename();
            String newFileName = subject.getId()+"r"+
                    filename.substring(filename.lastIndexOf("."));
            //System.out.println(newFileName);
            try {
                readfile.transferTo(new File(path + File.separator + newFileName));
                subject.setDmusicpath("/upload/music/"+newFileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



        MultipartFile zreadfile = murequest.getFile("zreadfile");
        if(!zreadfile.isEmpty()) {
            //上传文件路径
            String path = request.getServletContext().getRealPath("/upload/music/");
            //上传文件名
            String filename = zreadfile.getOriginalFilename();
            String newFileName =   subject.getId()+"zr"+
                    filename.substring(filename.lastIndexOf("."));
           // System.out.println(newFileName);
            try {
                zreadfile.transferTo(new File(path + File.separator + newFileName));
                subject.setZdmusicpath("/upload/music/"+newFileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        MultipartFile examfile = murequest.getFile("examfile");
        if(!examfile.isEmpty()) {
            //上传文件路径
            String path = request.getServletContext().getRealPath("/upload/music/");
            //上传文件名
            String filename = examfile.getOriginalFilename();
            String newFileName =  subject.getId()+"e"+
                    filename.substring(filename.lastIndexOf("."));
            try {
                examfile.transferTo(new File(path + File.separator + newFileName));
                subject.setMusicpath("/upload/music/"+newFileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        MultipartFile zexamfile = murequest.getFile("zexamfile");
        if(!zexamfile.isEmpty()) {
            //上传文件路径
            String path = request.getServletContext().getRealPath("/upload/music/");
            //上传文件名
            String filename = zexamfile.getOriginalFilename();
            String newFileName =subject.getId()+"ze"+
                    filename.substring(filename.lastIndexOf("."));
            try {
                zexamfile.transferTo(new File(path + File.separator + newFileName));
                subject.setZmusicpath("/upload/music/"+newFileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        MultipartFile mfile = murequest.getFile("mfile");
        //        -- 2 无  0 图片 1视频
        if(!mfile.isEmpty()) {
            System.out.println("我是技巧");
            if (subject.getMtype() != 2) {
                String path = null;
                String rpath=null;
                String newFileName="";
                //上传文件名
                String filename = mfile.getOriginalFilename();
                if (subject.getMtype() == 0) {
                    path = request.getServletContext().getRealPath("/upload/image/");
                    newFileName = subject.getId()+"mi"+
                            filename.substring(filename.lastIndexOf("."));
                    rpath = "/upload/image/";
                } else if (subject.getMtype() == 1) {
                    newFileName = subject.getId()+"mv"+
                            filename.substring(filename.lastIndexOf("."));
                    path = request.getServletContext().getRealPath("/upload/volice/");
                    rpath = "/upload/volice/";
                }
                try {
                    mfile.transferTo(new File(path + File.separator + newFileName));
                    subject.setMpath(rpath + newFileName);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        MultipartFile emfile = murequest.getFile("emfile");
        //        -- 2 无  0 图片 1视频
        if(!emfile.isEmpty()) {
            if (subject.getTmtype() != 2) {
                String path = null;
                String rpath=null;
                String newFileName="";
                //上传文件名
                String filename = emfile.getOriginalFilename();
                if (subject.getTmtype() == 0) {
                    path = request.getServletContext().getRealPath("/upload/image/");
                    newFileName = subject.getId()+"emi"+
                            filename.substring(filename.lastIndexOf("."));
                    rpath = "/upload/image/";
                } else if (subject.getTmtype() == 1) {
                    path = request.getServletContext().getRealPath("/upload/volice/");
                    newFileName = subject.getId()+"emv"+
                            filename.substring(filename.lastIndexOf("."));
                    rpath = "/upload/volice/";
                }
                try {
                    emfile.transferTo(new File(path + File.separator + newFileName));
                    subject.setTmpath(rpath + newFileName);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        carManagerService.modifySubject(subject);
       /* if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);*/


        if (result != null) {
            request.setAttribute("msg","操作成功");
        } else {
            request.setAttribute("msg","操作失败");
        }
        return "/admin/subject/subject_success";

    }

    /**
     * 删除一个题
     *
     * @param id
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @RequestMapping(value = "/subject", method = RequestMethod.DELETE)
    public void removeSubject(Integer id, HttpServletResponse response) {
        Integer result = carManagerService.removeSubject(id);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 删除一个或多个题
     *
     * @param ids
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @RequestMapping(value = "/subjects", method = RequestMethod.DELETE)
    public void removeSubjects(@RequestBody List<Integer> ids, HttpServletResponse response) {
        Integer result = carManagerService.removeSubjects(ids);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 修改一个题
     *
     //     * @param subject
     //     * @param readfile  读题音频
     //     * @param examfile  解析音频
     //     * @param zreadfile 藏读题音频
     //     * @param zexamfile 藏解析音频
     //     * @param mfile  媒体
     //     * @param emfile 解释媒体
     * @param request
     * @param response
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @RequestMapping(value = "/subject/fix", method = RequestMethod.POST)
    public String modifySubject(Subject subject, HttpServletRequest request, HttpServletResponse response) {
        Integer result = null;
        MultipartHttpServletRequest murequest=(MultipartHttpServletRequest)request;
        MultipartFile readfile = murequest.getFile("readfile");
        if(readfile!=null&&!readfile.isEmpty()) {
            //上传文件路径
            String path = request.getServletContext().getRealPath("/upload/music/");
            //上传文件名
            String filename = readfile.getOriginalFilename();
            String newFileName = (int)(Math.random()*10000000) +""+(int)(Math.random()*10000000)+
                    filename.substring(filename.lastIndexOf("."));
            System.out.println(newFileName);
            try {
                readfile.transferTo(new File(path + File.separator + newFileName));
                subject.setDmusicpath("/upload/music/"+newFileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



        MultipartFile zreadfile = murequest.getFile("zreadfile");
        if(zreadfile!=null&&!zreadfile.isEmpty()) {
            //上传文件路径
            String path = request.getServletContext().getRealPath("/upload/music/");
            //上传文件名
            String filename = zreadfile.getOriginalFilename();
            String newFileName = (int)(Math.random()*10000000) +""+(int)(Math.random()*10000000)+
                    filename.substring(filename.lastIndexOf("."))+"fdsa";
            System.out.println(newFileName);
            try {
                zreadfile.transferTo(new File(path + File.separator + newFileName));
                subject.setZdmusicpath("/upload/music/"+newFileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



        MultipartFile examfile = murequest.getFile("examfile");
        if(examfile!=null&&!examfile.isEmpty()) {
            //上传文件路径
            String path = request.getServletContext().getRealPath("/upload/music/");
            //上传文件名
            String filename = examfile.getOriginalFilename();
            String newFileName = (int)(Math.random()*10000000) +""+(int)(Math.random()*10000000)+
                    filename.substring(filename.lastIndexOf("."));
            try {
                examfile.transferTo(new File(path + File.separator + newFileName));
                subject.setMusicpath("/upload/music/"+newFileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        MultipartFile zexamfile = murequest.getFile("zexamfile");
        if(zexamfile!=null&&!zexamfile.isEmpty()) {
            //上传文件路径
            String path = request.getServletContext().getRealPath("/upload/music/");
            //上传文件名
            String filename = zexamfile.getOriginalFilename();
            String newFileName = (int)(Math.random()*10000000) +""+(int)(Math.random()*10000000)+
                    filename.substring(filename.lastIndexOf("."));
            try {
                zexamfile.transferTo(new File(path + File.separator + newFileName));
                subject.setZmusicpath("/upload/music/"+newFileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        MultipartFile mfile = murequest.getFile("mfile");
        //        -- 2 无  0 图片 1视频
        if(mfile!=null&&!mfile.isEmpty()) {
            if (subject.getMtype() != 2) {
                String path = null;
                String rpath=null;
                //上传文件路径
                if (subject.getMtype() == 0) {
                    path = request.getServletContext().getRealPath("/upload/image/");
                    rpath = "/upload/image/";
                } else if (subject.getMtype() == 1) {
                    path = request.getServletContext().getRealPath("/upload/volice/");
                    rpath = "/upload/volice/";
                }
                //上传文件名
                String filename = mfile.getOriginalFilename();
                String newFileName = (int) (Math.random() * 10000000) + "" + (int) (Math.random() * 10000000) +
                        filename.substring(filename.lastIndexOf("."));
                try {
                    mfile.transferTo(new File(path + File.separator + newFileName));
                    subject.setMpath(rpath + newFileName);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        MultipartFile emfile = murequest.getFile("emfile");
        //        -- 2 无  0 图片 1视频
        if(emfile!=null&&!emfile.isEmpty()) {
            if (subject.getTmtype() != 2) {
                String path = null;
                String rpath=null;
                //上传文件路径
                if (subject.getTmtype() == 0) {
                    path = request.getServletContext().getRealPath("/upload/image/");
                    rpath = "/upload/image/";
                } else if (subject.getTmtype() == 1) {
                    path = request.getServletContext().getRealPath("/upload/volice/");
                    rpath = "/upload/volice/";
                }
                //上传文件名
                String filename = emfile.getOriginalFilename();
                String newFileName = (int) (Math.random() * 10000000) + "" + (int) (Math.random() * 10000000) +
                        filename.substring(filename.lastIndexOf("."));
                try {
                    emfile.transferTo(new File(path + File.separator + newFileName));
                    subject.setTmpath(rpath + newFileName);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        result = carManagerService.modifySubject(subject);

      /*  if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);*/
        //return "";

        if (result != null) {
            request.setAttribute("msg","操作成功");
        } else {
            request.setAttribute("msg","操作失败");
        }
        return "/admin/subject/subject_success";
    }


    /**
     * 根据套题id查找题目
     *
     * @param seid
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @RequestMapping(value = "/subjectBySeid", method = RequestMethod.GET)
    public void getSubjectBySeId(Integer seid, HttpServletResponse response) {
        List<SubjectVo> result = carManagerService.getSubjectBySeId(seid);
        if (result != null) {
            JSONModel.put("data", result);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 查找所有题
     *
     * @param pageSize
     * @param pageNum
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @RequestMapping(value = "/subject", method = RequestMethod.GET)
    public void getSubjects(Integer pageNum, Integer pageSize, HttpServletResponse response) {
        pageNum = pageNum != null ? pageNum : 1;
        pageSize = (pageSize != null) ? pageSize : 10;
        PageHelper.startPage(pageNum, pageSize, true);
        List<Subject> result = carManagerService.getSubjects();
        PageInfo pageInfo = new PageInfo(result);
        if (result != null) {
            JSONModel.put("data", pageInfo);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 根据题目id查找题目加上题目所属的所有父级
     *
     * @param id
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @RequestMapping(value = "/subjectById", method = RequestMethod.GET)
    public void getSubjectById(Integer id, HttpServletResponse response) {
        Subject result = carManagerService.getSubjectById(id);
        List<CartypeVoEnhance> list = carManagerService.getSubjectBySubId(id);
        if (result != null) {
            JSONModel.put("data", result);
            JSONModel.put("location", list);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 查找对应类型的所有题
     *@param  type 0单选 1判断 2多选
     * @param pageSize
     * @param pageNum
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @RequestMapping(value = "/subjectByType", method = RequestMethod.GET)
    public void getSubjectsByType(Integer pageNum, Integer pageSize,Integer type, HttpServletResponse response) {
        pageNum = pageNum != null ? pageNum : 1;
        pageSize = (pageSize != null) ? pageSize : 10;
        PageHelper.startPage(pageNum, pageSize, true);
        List<Subject> result = carManagerService.getSubjectsByType(type);
        PageInfo pageInfo = new PageInfo(result);
        if (result != null) {
            JSONModel.put("data", pageInfo);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 关键词或者ID查找题
     *@param  keyCode 查询条件
     * @param pageSize
     * @param pageNum
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @RequestMapping(value = "/seekSubjects", method = RequestMethod.GET)
    public void seekSubjects(Integer pageNum, Integer pageSize,String keyCode, HttpServletResponse response) {
        pageNum = pageNum != null ? pageNum : 1;
        pageSize = (pageSize != null) ? pageSize : 10;
        PageHelper.startPage(pageNum, pageSize, true);
        List<Subject> result = carManagerService.seekSubjects(keyCode);
        PageInfo pageInfo = new PageInfo(result);
        if (result != null) {
            JSONModel.put("data", pageInfo);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

}
